<?php

class SupplierLocationOrderAction extends CommonAction{
public function index()
{
if(trim($_REQUEST['status'])!='-1'&&trim($_REQUEST['status'])!='')
{
$map['status'] = array('eq',trim($_REQUEST['status']));
}
if(trim($_REQUEST['order_sn'])!='')
{
$map['order_sn'] = array('eq',trim($_REQUEST['order_sn']));
}
if(intval($_REQUEST['sid']))
{
$map['sid'] = array('eq',intval($_REQUEST['sid']));
}
if(intval($_REQUEST['timetype'])==2){
$time_filed = 'create_time';
}
else
{
$time_filed = 'order_time';
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])!=''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$end_time = to_timespan(trim($_REQUEST['enddate']));
$map[$time_filed]= array('between',array($start_time,$end_time));
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])==''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$map[$time_filed]= array('egt',$start_time);
}
if(trim($_REQUEST['begindate'])==''&&trim($_REQUEST['enddate'])!=''){
$end_time = to_timespan(trim($_REQUEST['enddate']));
$map[$time_filed]= array('elt',$end_time);
}
if(trim($_REQUEST['deal_type'])!='-1'&&trim($_REQUEST['deal_type'])!='')
{
$map['deal_type'] = array('eq',trim($_REQUEST['deal_type']));
}
if($this->get('default_map'))
$map = array_merge($map,$this->get('default_map'));
if (method_exists ( $this,'_filter')) {
$this->_filter ( $map );
}
$name=$this->getActionName();
$model = D ($name);
if (!empty ( $model )) {
$this->_list ( $model,$map );
}
$this->display ();
return;
}
public function view_order(){
$id = intval($_REQUEST ['id']);
$condition['id'] = $id;
$order_info =  M(MODULE_NAME)->where($condition)->find();
$this->assign('order_info',$order_info);
$this->display ();
}
public function step1(){
$id =  intval($_REQUEST['id']);
if(D(MODULE_NAME)->where('id='.$id)->setField('status',1)){
$order_info =  M(MODULE_NAME)->where('id='.$id)->find();
if($order_info){
if($order_info['deal_type'] ==1)
$tmpl = $GLOBALS['db']->getRow('select * from '.DB_PREFIX."msg_template where name = 'TPL_SMS_STORE_KTV_ORDER'");
else
$tmpl = $GLOBALS['db']->getRow('select * from '.DB_PREFIX."msg_template where name = 'TPL_SMS_STORE_ORDER'");
$tmpl_content = $tmpl['content'];
$location = $GLOBALS['db']->getRow('SELECT `name`,`address`,`tel`,`is_bank` FROM '.DB_PREFIX.'supplier_location WHERE id='.$order_info['sid']);
$sms_order['user_name'] = $order_info['order_name'];
$sms_order['user_name'] .= $order_info['order_sex'] ==1 ?'先生': '女士';
$sms_order['location_name'] = $location['name'];
$sms_order['order_time'] = to_date($order_info['order_time'],'m月d日H点i分');
$sms_order['info'] = $order_info['seat_status'] == 0 ?'大厅': '包厢'.$order_info['order_nums'] .'位';
if($order_info['extra_serve']==1)
$sms_order['info'] .= '，'.'吸烟区';
elseif($order_info['extra_serve']==2)
$sms_order['info'] .= '，'.'无烟区';
if($location['is_bank']==1)
$sms_order['info'] .= '，'.'支持银行卡支付';
$sms_order['address'] = $location['address'];
$sms_order['tel'] = $location['tel'];
$GLOBALS['tmpl']->assign('order',$sms_order);
$msg = $GLOBALS['tmpl']->fetch('str:'.$tmpl_content);
$msg_data['send_type'] = 0;
$msg_data['content'] = addslashes($msg);
$msg_data['send_time'] = 0;
$msg_data['is_send'] = 0;
$msg_data['create_time'] = get_gmtime();
$msg_data['user_id'] = intval($order_info['user_id']);
$msg_data['is_html'] = 0;
$msg_data['is_youhui'] = 2;
$msg_data['dest'] = $order_info['order_tel'];
$GLOBALS['db']->autoExecute(DB_PREFIX.'deal_msg_list',$msg_data,'INSERT');
$tel_msg_data['send_type'] = 0;
$tel_msg_data['content'] = addslashes($order_info['order_sms_message']);
$tel_msg_data['send_time'] = 0;
$tel_msg_data['is_send'] = 0;
$tel_msg_data['create_time'] = get_gmtime();
$tel_msg_data['user_id'] = intval($order_info['user_id']);
$tel_msg_data['is_html'] = 0;
$tel_msg_data['is_youhui'] = 4;
$tel_list = explode(';',$order_info['order_sms_tel']);
foreach($tel_list as $k=>$v){
if(!empty($v)){
$tel_msg_data['dest'] = $v;
$GLOBALS['db']->autoExecute(DB_PREFIX.'deal_msg_list',$tel_msg_data,'INSERT');
}
}
$email_msg_data['send_type'] = 1;
$email_msg_data['content'] = addslashes($order_info['order_email_message']);
$email_msg_data['send_time'] = 0;
$email_msg_data['is_send'] = 0;
$email_msg_data['create_time'] = get_gmtime();
$email_msg_data['user_id'] = intval($GLOBALS['user_info']['id']);
$email_msg_data['is_html'] = 0;
$email_msg_data['is_youhui'] = 4;
$email_list = explode(';',$order_info['order_email_address']);
foreach($email_list as $k=>$v){
if(!empty($v)){
$email_msg_data['dest'] = $v;
$GLOBALS['db']->autoExecute(DB_PREFIX.'deal_msg_list',$email_msg_data,'INSERT');
}
}
}
}
$this->success(L('UPDATE_SUCCESS'));
}
public function daodabox(){
$id =  intval($_REQUEST['id']);
$this->assign('order_id',$id);
$this->display ();
}
public function do_daoda(){
$id =  intval($_REQUEST['order_id']);
$order_money = $_REQUEST['order_money'];
D(MODULE_NAME)->where('id='.$id)->setField(array('status','order_money'),array(2,$order_money));
$this->success(L('UPDATE_SUCCESS'));
}
public function cancelbox(){
$id =  intval($_REQUEST['id']);
$this->assign('order_id',$id);
$this->display ();
}
public function do_cancel(){
$id =  intval($_REQUEST['order_id']);
$order_tip = $_REQUEST['order_tip'];
D(MODULE_NAME)->where('id='.$id)->setField(array('status','order_tip'),array(4,$order_tip));
save_log('撤销餐厅预订!',1);
$this->success(L('UPDATE_SUCCESS'));
}
public function export_csv($page = 1)
{
set_time_limit(0);
$limit = (($page -1)*intval(app_conf('BATCH_PAGE_SIZE'))).','.(intval(app_conf('BATCH_PAGE_SIZE')));
if(!isset($_REQUEST['status']))
{
$_REQUEST['status']=-1;
}
if(!isset($_REQUEST['deal_type']))
{
$_REQUEST['deal_type']=-1;
}
$where = ' 1=1 ';
if(intval($_REQUEST['sid']))
{
$where.=' and sid='.intval($_REQUEST['sid']).'';
}
if(trim($_REQUEST['order_sn']) !='')
{
$where.=' and order_sn='.trim($_REQUEST['order_sn']).'';
}
if(intval($_REQUEST['status']) >= 0)
{
$where.=' and status='.intval($_REQUEST['status']).'';
}
if(intval($_REQUEST['deal_type']) >= 0)
{
$where.=' and deal_type='.intval($_REQUEST['deal_type']).'';
}
if(intval($_REQUEST['timetype'])==2){
$time_filed = 'create_time';
}
else
{
$time_filed = 'order_time';
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])!=''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$end_time = to_timespan(trim($_REQUEST['enddate']));
$where.=' and '.$time_filed.' between '.$start_time.' and '.$end_time.' ';
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])==''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$where.=' and '.$time_filed.' >'.$start_time.'';
}
if(trim($_REQUEST['begindate'])==''&&trim($_REQUEST['enddate'])!=''){
$end_time = to_timespan(trim($_REQUEST['enddate']));
$where.=' and '.$time_filed.' <'.$time_filed.'';
}
$list=M('SupplierLocationOrder')
->where($where)
->field(DB_PREFIX.'Supplier_location_order.*')
->limit($limit)->findAll();
if($list)
{
register_shutdown_function(array(&$this,'export_csv'),$page+1);
$order_value=array('sn'=>'""','supplier_location_name'=>'""','order_time'=>'""','create_time'=>'""','order_name'=>'""','order_tel'=>'""','user_name'=>'""','status'=>'""','order_money'=>'""','room_type'=>'""','order_nums'=>'""','order_tip'=>'""','order_msg'=>'""');
if($page == 1)
{
$content=iconv('utf-8','gbk','订单号,餐厅,预订时间,下单时间,联系人,联系电话,会员,订单状态,消费,包房需求,人数,失败原因,其他信息');
$content = $content ."\n";
}
foreach($list as $k=>$v)
{
$order_value['sn']='"'.'sn:'.iconv('utf-8','gbk',$v['order_sn']) .'"';
$supplier_location = M('SupplierLocation')->getById($v['sid']);
$order_value['supplier_location_name'] = '"'.iconv('utf-8','gbk',$supplier_location['name']) .'"';
$order_value['order_time'] = '"'.iconv('utf-8','gbk',to_date($v['order_time'])) .'"';
$order_value['create_time'] = '"'.iconv('utf-8','gbk',to_date($v['create_time'])) .'"';
if($v['sex']==1)
$order_value['order_name'] = '"'.iconv('utf-8','gbk',$v['order_name'].'先生') .'"';
else
$order_value['order_name'] = '"'.iconv('utf-8','gbk',$v['order_name'].'女士') .'"';
$order_value['order_tel'] = '"'.iconv('utf-8','gbk',$v['order_tel']) .'"';
$user_info = M('User')->getById($v['user_id']);
$order_value['user_name'] = '"'.iconv('utf-8','gbk',$user_info['user_name']) .'"';
$order_value['status'] = '"'.iconv('utf-8','gbk',get_order_status($v['status'])) .'"';
$order_value['order_money'] = '"'.iconv('utf-8','gbk',format_price($v['order_money'])) .'"';
if($v['deal_type'] ==1)
{
if($v['seat_status']==0)
$room_type='小包箱';
elseif($v['seat_status']==1)
$room_type='中包箱';
elseif($v['seat_status']==2)
$room_type='大包箱';
elseif($v['seat_status']==3)
$room_type='VIP包箱';
}
else
{
if($v['seat_status']==0)
{
$room_type='只订大厅';
}
else
{
$room_type='包箱';
if($v['seat_switch']==0)
$room_type.='(订不到包房就订大厅)';
}
if($v['extra_serve'] ==1)	
$room_type.=',要吸烟区';
elseif($v['extra_serve'] ==2)
$room_type.=',不要吸烟区';
if($v['min_expense']==500)
$room_type.=',接受最低消费500以内';
elseif($v['min_expense']==1000)
$room_type.=',接受最低消费501到1000';
elseif($v['min_expense']==2000)
$room_type.=',接受最低消费1001到2000';
elseif($v['min_expense']==5000)
$room_type.=',接受最低消费2001到5000';
else
$room_type.=',没有最低消费';
}
$order_value['room_type'] = '"'.iconv('utf-8','gbk',$room_type) .'"';
$order_value['order_nums'] = '"'.iconv('utf-8','gbk',$v['order_nums']) .'"';
$order_value['order_tip'] = '"'.iconv('utf-8','gbk',$v['order_tip']) .'"';
$order_value['order_msg'] = '"'.iconv('utf-8','gbk',$v['order_msg']) .'"';
$content .= implode(',',$order_value) ."\n";
}
header('Content-Disposition: attachment; filename=supplier_location_order_list.csv');
echo $content;
}
else
{
if($page==1)
$this->error(L('NO_RESULT'));
}
}
}
function get_order_status($status,$type){
if(intval($type)==0)
return l('STORE_ORDER_STATUS_'.$status);
else
return l('STORE_ORDER_KTV_STATUS_'.$status);
}
function get_location_name($sid){
return M('SupplierLocation')->where('id='.$sid)->getField('name');
}

?>